<template>
  <div class="login-content">
    <div class="loginbox">
      <div class="avater">
        <img src="@/assets/logo.png" alt>
      </div>
      <el-form
        ref="loginForm"
        label-width="80px"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-position="left"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            ref="username"
            placeholder="Username"
            prefix-icon="iconfont icon-plant-1"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            ref="password"
            :type="passwordType"
            placeholder="Password"
            prefix-icon="iconfont icon-plant-1"
            v-model="loginForm.password"
          >            
            <i slot="suffix" :class="['show-pwd', 'iconfont', passwordType=='text'?'icon-plant-3':'icon-eye']"  @click="showPwd"></i>
          </el-input>
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;margin-top:10px;" @click.native.prevent="handleLogin">Login</el-button>
      </el-form>
    </div>
  </div>
</template>
<script>
import { validUsername } from "@/utils/validate.js";
import {mapActions} from 'vuex'
import axios from 'axios'
export default {
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("Please enter the correct user name"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("The password can not be less than 6 digits"));
      } else {
        callback();
      }
    };
    return {
      loginForm: { username: "admin", password: "111111" },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword }
        ]
      },
      passwordType: "password",
      loading:false
    };
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    ...mapActions(['login']),
    showPwd(){
      if (this.passwordType === 'password') {
        this.passwordType = 'text'
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin(){
      axios.post('/login',{username:'zyr',password:111111}).then(res=>{
        console.log(res,999);
        
      })
      // this.$refs.loginForm.validate(valid=>{
      //   if(valid){
      //     this.loading = true
      //     // this.$store.dispatch('login',this.loginForm)
      //     this.login('login',this.loginForm).then((res)=>{
      //       // this.$store.dispatch('getInfo')
      //       this.$router.push('/dashboard')
      //       this.loading = false
      //     }).catch(()=>{
      //       this.loading = false
      //     })

      //   }else{
      //     this.$message.error('登录有误！')
      //   }
      // })
    }
  },
};
</script>
<style lang="less" scoped>
.login-content {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: green;
  .loginbox {
    flex: 0 1 500px;
    height: 300px;
    background: #fff;
    position: relative;
    .avater {
      position: absolute;
      left: 50%;
      top: 0;
      transform: translate(-50%, -50%);
      width: 20%;
      height: 20%;
      border-radius: 50%;
      border: 1px solid #eee;
      padding: 10px;
      box-shadow: 0 0 10px #ccc;
      background: #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .el-form {
      position: absolute;
      width: 100%;
      padding: 20px;
      bottom: 0px;
      box-sizing: border-box;
      .show-pwd {
        cursor: pointer;
      }
    }
  }
}
</style>
